<template>
  <div class="xiecheng">
    <div class="admin_main_block">
      <div class="admin_main_block_top">
        <div class="admin_main_block_left">
          <div>{{ i18n('organization.apply.info') }}</div>
        </div>

        <div class="admin_main_block_right">
          <div>
            <el-button icon="Back" @click="router.go(-1)">
              {{ i18n('common.return') }}
            </el-button>
          </div>
        </div>
      </div>

      <div class="admin_form_main">
        <el-form label-width="150px" ref="formRef" :model="dataForm" :rules="dataRule">
          <el-card class="box-card" shadow="never" v-if="dataForm.organization">
            <template v-slot:header>
              <div class="clearfix">
                <span>{{ i18n('organization.apply.organization_info') }}</span>
              </div>
            </template>
            <div class="text item">
              <el-row>
                <el-col class="text-center" :span="3">
                  <el-image class="cx-view-preview" :src="dataForm.organization.logo">
                    <template v-slot:error>
                      <div class="image-slot">
                        <el-icon><Picture /></el-icon>
                      </div>
                    </template>
                  </el-image>
                </el-col>
                <el-col :span="6">
                  <el-descriptions border>
                    <el-descriptions-item width="200" :label="i18n('organization.chinese_title')" label-align="right" :span="3">
                      {{ dataForm.organization.chinese_title }}
                    </el-descriptions-item>

                    <el-descriptions-item width="200" :label="i18n('organization.english_title')" label-align="right" :span="3">
                      {{ dataForm.organization.english_title }}
                    </el-descriptions-item>

                    <el-descriptions-item width="200" :label="i18n('organization.register_limit')" label-align="right" :span="3">
                      {{ dataForm.organization.register_limit }}
                    </el-descriptions-item>

                    <el-descriptions-item width="200" :label="i18n('organization.valid_time')" label-align="right" :span="3">
                      {{ dataForm.organization.valid_time }}
                    </el-descriptions-item>
                  </el-descriptions>
                </el-col>
              </el-row>
            </div>
          </el-card>

          <el-card class="box-card mt10" shadow="never" v-if="dataForm.member">
            <template v-slot:header>
              <div class="clearfix">
                <span>{{ i18n('organization.apply.member_info') }}</span>
              </div>
            </template>
            <div class="text item">
              <el-row>
                <el-col class="text-center" :span="3">
                  <el-image class="cx-view-preview" :src="dataForm.member.avatar">
                    <template v-slot:error>
                      <div class="image-slot">
                        <el-icon><Picture /></el-icon>
                      </div>
                    </template>
                  </el-image>
                </el-col>
                <el-col :span="6">
                  <el-descriptions border>
                    <el-descriptions-item width="200" :label="i18n('member.chinese_nickname')" label-align="right" :span="3">
                      {{ dataForm.member.chinese_nickname }}
                    </el-descriptions-item>

                    <el-descriptions-item width="200" :label="i18n('member.chinese_mobile')" label-align="right" :span="3">
                      {{ dataForm.member.chinese_mobile }}
                    </el-descriptions-item>

                    <el-descriptions-item width="200" :label="i18n('member.create_time')" label-align="right" :span="3">
                      {{ dataForm.member.create_time }}
                    </el-descriptions-item>
                  </el-descriptions>
                </el-col>
              </el-row>
            </div>
          </el-card>

          <el-card class="box-card mt10" shadow="never">
            <template v-slot:header>
              <div class="clearfix">
                <span>{{ i18n('organization.apply.resource.info') }}</span>
              </div>
            </template>
            <div class="text item text-center">
              <el-descriptions border>
                <el-descriptions-item width="200" :label="i18n('organization.apply.inquiry_harbor')" label-align="right" :span="3">
                  {{ dataForm.inquiry_harbor }}
                </el-descriptions-item>
                <el-descriptions-item width="200" :label="i18n('organization.apply.resource.url')" label-align="right" :span="3">
                  <el-row :gutter="20">
                    <el-col :span="2" v-for="(v, k) in dataForm.resource" :key="k">
                      <view class="grid">
                        <el-link :href="v.url" :underline="false" target="_blank">
                          <img v-if="v.url" src="@/assets/images/default/file.png" class="avatar-upload"/>
                        </el-link>
                        <el-text class="filename" line-clamp="1">
                          {{ v.filename }}
                        </el-text>
                      </view>
                    </el-col>
                  </el-row>
                </el-descriptions-item>
              </el-descriptions>

            </div>
          </el-card>

          <el-card class="box-card mt10" shadow="never" v-if="dataForm.organization">
            <template v-slot:header>
              <div class="clearfix">
                <span>{{ i18n('organization.apply.info') }}</span>
              </div>
            </template>
            <div class="text item">
              <el-form-item :label="i18n('organization.apply.create_time')">
                <el-text>
                  {{ dataForm.create_time }}
                </el-text>
              </el-form-item>

              <el-form-item :label="i18n('organization.apply.apply_status')" prop="apply_status">
                <el-select v-model="dataForm.apply_status" :placeholder="$t('common.please_select') + $t('common.apply_status')" clearable>
                  <el-option v-for="(v, k) in applyStatusList" :label="v.text" :key="k" :value="v.value">
                  </el-option>
                </el-select>
              </el-form-item>

              <el-form-item :label="i18n('organization.inquiry_valid_time')" prop="inquiry_valid_time">
                <el-date-picker v-model="dataForm.inquiry_valid_time" type="date" :disabled-date="disabledDate" :placeholder="i18n('common.please_input') + i18n('organization.inquiry_valid_time')" clearable/>
              </el-form-item>

              <el-form-item :label="i18n('organization.apply.remark')" prop="remark">
                <el-input type="textarea" :placeholder="i18n('common.please_input') + i18n('organization.apply.remark')" v-model="dataForm.remark" rows="3" show-word-limit maxlength="200"></el-input>
              </el-form-item>
            </div>
          </el-card>

          <el-card class="box-card mt10" shadow="never">
            <div class="text item">
              <el-form-item>
                <el-button
                  v-if="isAuth('module:organization:apply:audit')"
                  type="primary"
                  @click="dataFormSubmit(formRef)">
                  {{ i18n('common.confirm') }}
                </el-button>
                <el-button @click="resetForm(formRef)">
                  {{ i18n('common.reset') }}
                </el-button>
              </el-form-item>
            </div>
          </el-card>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup name="organization-apply-audit">
  import { useBase } from "@/hooks/base/useBase"
  import { useForm } from "@/hooks/form/useForm"

  const { router, query, i18n, isAuth } = useBase()

  const model = ref('organization/apply')

  const current_time = ref('')
  const applyStatusList = ref([])

  const dataForm = reactive({
    id: 0,
    organization_id: 0,
    member: {},
    organization: {},
    remark: '',
    create_time: '',
    apply_status: '1',
    inquiry_valid_time: '',
    resource: [],
  })

  const dataRule = reactive({
    audit_status: [{
      required: true,
      message: i18n('organization.apply.rules.apply_status.require'),
      trigger: 'blur',
    }],
  })

  const {
    formRef,
    resetForm
  } = useForm(model.value, dataForm)

  const init = (form) => {
    dataForm.id = query.id || 0

    nextTick(() => {
      form.value.resetFields()
      if (dataForm.id) {
        $http({
          url: $http.adornUrl(`/${model.value}/view/${dataForm.id}`),
          method: 'get',
          params: $http.adornParams({}),
        }).then(({ data }) => {
          if (data && data.status === 200) {
            dataForm.member = data.data.member
            dataForm.resource = data.data.resource
            dataForm.organization_id = data.data.organization_id
            dataForm.inquiry_harbor = data.data.inquiry_harbor
            dataForm.remark = data.data.remark
            dataForm.create_time = data.data.create_time
          }
        }).then(() => {
          if (dataForm.id) {
            $http({
              url: $http.adornUrl(`/organization/data`),
              method: 'get',
              params: $http.adornParams({
                'id': dataForm.organization_id
              }),
            }).then(({ data }) => {
              if (data && data.data && data.status === 200) {
                dataForm.organization = data.data
                dataForm.inquiry_valid_time = data.data.inquiry_valid_time
              }
            })
          }
        })
      }
    })
  }

  const dataFormSubmit = (form) => {
    form.validate(async (valid, fields) => {
      if (valid) {
        $http({
          url: $http.adornUrl('/organization/apply/audit'),
          method: 'post',
          data: $http.adornData(dataForm),
        }).then(({ data }) => {
          if (data && data.status === 200) {
            $message.success(i18n('common.handle_success'))
            router.go(-1)
          } else {
            $message.error(data.message)
          }
        })
      }
    })
  }

  const loadApplyStatusList = () => {
    $http({
      url: $http.adornUrl('/dictionary/data'),
      method: 'get',
      params: $http.adornParams({
        title: 'apply_status',
      }),
    }).then(({data}) => {
      if (data && data.status === 200) {
        applyStatusList.value = data.data
      } else {
        $message.error(data.message)
      }
    })
  }

  const disabledDate = (time) => {
    return time.getTime() - 24 * 60 * 60 * 1000 * 30 < current_time.value
  }

  onMounted(() => {
    current_time.value = Date.now()

    init(formRef)

    loadApplyStatusList()
  })
</script>

<style lang="scss" scoped>
  .cx-view-preview {
    padding: 0 5px;
    max-width: 120px;
    max-height: 120px;
    width: 100%;
    height: 120px;
  }
  .admin_form_main .el-form-item {
    width: 100%;
  }
  .grid {
    display: grid;
    text-align: center;
    .filename {
      font-size: 14px;
      color: #666;
    }
  }
</style>
